package com.afiorenza.two.impl;

import com.afiorenza.two.Generator;

public class PrimeGenerator implements Generator {
	long last = 1;

	@Override
	public long next() {
		return nextPrime();
	}

	private long nextPrime() {

		while (!isPrimo(last)) {
			last++;
		}
		return last++;

	}

	private boolean isPrimo(long x) {
		if (x == 1)
			return false;
		if (x == 2 || x == 3)
			return true;
		if (x % 2 == 0 || x % 3 == 0)
			return false;

		long hasta = (long) (Math.sqrt(x) + 1);
		for (long i = 6; i < hasta; i += 6) {
			if ((x % (i - 1)) == 0 || x % (i + 1) == 0) {
				return false;
			}
		}
		return true;
	}
}